package worker;

import java.io.IOException;

import org.objectweb.proactive.api.PAActiveObject;
import org.objectweb.proactive.core.ProActiveException;

public class MainWorker {

	/**
	 * @param args
	 * @throws ProActiveException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws ProActiveException, IOException {

		if (args.length > 1) {
			System.out.println("Usage : MainWorker url_of_entrypoint");
			return;
		}


		if (args.length == 0) {
			// Création du premier Worker
			ManagerImpl manager = PAActiveObject.newActive(ManagerImpl.class, null);
			System.out.println(PAActiveObject.registerByName(manager, "entryPoint", true));
			manager.init();
		}
		else {
			// Ajout d'un worker au réseau
			try {
				ManagerImpl ref = PAActiveObject.lookupActive(ManagerImpl.class, "//jerome-laptop.local:1099/entryPoint" /*args[0]*/);
				ManagerImpl manager = PAActiveObject.newActive(ManagerImpl.class, null);
				manager.connectTo(ref);
			}
			catch (ProActiveException e) {
				System.err.println("Erreur lors de la création du manager");
			}
			catch (IOException e) {
				System.err.println("Erreur lors du lookup du point d'entrée");
			}
		}
	}

}
